<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新工具</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" :inline="true" :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="6" :sm="8">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="6" :sm="8">
              <el-form-item prop="brand">
                <div>品牌</div>
                <el-input v-model="data.brand" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="6" :sm="8">
              <el-form-item prop="spec">
                <div>型号</div>
                <el-input v-model="data.spec" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="cost_id">
                <div>成本中心</div>
                <v-select v-model="data.cost_id" action="costList" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="unit">
                <div>单位</div>
                <el-input v-model="data.unit" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="number">
                <div>当前数量</div>
                <el-input v-model="data.number" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="invisible_number">
                <div>不在帐数量</div>
                <el-input v-model="data.invisible_number" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="price">
                <div>价格</div>
                <el-input v-model="data.price" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="2" :lg="4" :md="6" :sm="8">
              <el-form-item prop="category_id">
                <div>分类</div>
                <v-select v-model="data.category_id" action="toolCategoryList" :paging="false" />
              </el-form-item>
            </el-col>
            <el-col :xl="2" :lg="4" :md="6" :sm="8">
              <el-form-item prop="store_id">
                <div>仓库</div>
                <v-select v-model="data.store_id" action="storeList" :query="{kind:1}" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="6" :sm="8">
              <el-form-item prop="status">
                <div>状态</div>
                <el-select v-model="data.status" size="mini">
                  <el-option v-for="(item, index) in commonStatus" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="replace_coding">
                <div>可替换编码</div>
                <el-input v-model="data.replace_coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="6" :sm="8">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :xl="4" :lg="4" :md="4" :sm="4">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                  </div>
                </div>
              </el-col>
              <el-col :xl="20" :lg="20" :md="20" :sm="20">
                <Tinymce id="content" v-model="data.content" />
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="供应商">
            <template>
              <el-table :data="provider">
                <el-table-column prop="providers.name" label="供应商" min-width="37%" />
                <el-table-column prop="providers.coding" label="参考编号" min-width="30%" />
                <el-table-column label="是供应商" min-width="8%">
                  <template v-slot="{row}">{{ row.providers.is_provider === 1?'是':'否' }}</template>
                </el-table-column>
                <el-table-column label="是制造商" min-width="8%">
                  <template slot-scope="{row}">{{ row.providers.is_manufacturer === 1?'是':'否' }}</template>
                </el-table-column>
                <el-table-column label="是外包商" min-width="9%">
                  <template slot-scope="{row}">{{ row.providers.is_epiboly === 1? '是':'否' }}</template>
                </el-table-column>
                <el-table-column label="操作" min-width="8%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="providerDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="append_provider" :model="providerSelect" :inline="true" class="hf_form append_other_admin" :rules="appendProvider">
                    <el-row :gutter="10">
                      <el-col :span="9">
                        <el-form-item prop="provider_id">
                          <v-select-s v-model="providerSelect.provider_id" action="onlyProviderList" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="7">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="providerSave" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </template>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/tool/tool'
import { commonStatus } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
export default {
  inject: ['backUrl', 'goUrl', 'delOne', 'update'],
  components: {
    Tinymce,
    VSelect,
    VSelectS
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        }
      },
      provider: [],
      providerSelect: {},
      commonStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入备件名称',
            trigger: 'blur'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入品牌',
            trigger: 'blur'
          }
        ],
        spec: [
          {
            required: true,
            message: '请输入型号',
            trigger: 'blur'
          }
        ],
        cost_id: [
          {
            required: true,
            message: '请选择成本中心',
            trigger: 'change'
          }
        ],
        unit: [
          {
            required: true,
            message: '请输入单位',
            trigger: 'blur'
          }
        ],
        number: [
          {
            required: true,
            message: '请输入数量',
            trigger: 'blur'
          }
        ],
        invisible_number: [
          {
            required: true,
            message: '请输入不在帐数量',
            trigger: 'blur'
          }
        ],
        price: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }
        ],
        category: [
          {
            required: true,
            message: '请选择分类',
            trigger: 'change'
          }
        ],
        store_id: [
          {
            required: true,
            message: '请选择仓库',
            trigger: 'change'
          }
        ],
        store_location_id: [
          {
            required: true,
            message: '请选择库位',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      appendProvider: {
        provider_id: [
          {
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }
        ]
      },
      eqRules: {
        equipment_id: [
          {
            required: true,
            message: '请选择资产',
            trigger: 'change'
          }
        ],
        notice: [
          {
            required: true,
            message: '请输入注意事项',
            trigger: 'blur'
          }
        ],
        number: [
          {
            required: true,
            message: '请输入数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 82404) {
        this.$alert('您请求的工具不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.provider = response.data.provider
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    providerSave() {
      this.$refs.append_provider.validate((valid) => {
        if (valid) {
          const provider_id = this.providerSelect.provider_id
          const tool_provider = {
            'provider_id': provider_id,
            'tool_id': this.$route.params.id
          }
          API.toolProvider(tool_provider).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.provider = response.data.provider
              })
            } else if (response.data.error_code === 82004) {
              this.$message.error('该供应商已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    providerDel(id) {
      const del_tool_provider = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delToolProvider(del_tool_provider).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.provider = response.data.provider
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    goBack() {
      this.goUrl('/tool/tool')
    }
  }
}
</script>
